package com.cdy.jxc_project.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.cdy.jxc_project.domain.ExportGoods;
import com.cdy.jxc_project.domain.TGoods;
import com.cdy.jxc_project.json.R;
import com.cdy.jxc_project.service.TGoodsService;

import com.cdy.jxc_project.utils.AddSysLog;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;

import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

/*
 * @Project:jxc_project
 * @Author:cdy(缘生)
 * @Motto:放下杂念,只为迎接明天更好的自己
 * @Date:2023/4/30 15:27
 * */
@Controller
@RequestMapping("/export")
public class ExportController {

    @Resource
    private TGoodsService tGoodsService;
    @AddSysLog
    @RequestMapping("/outputExport/{pId}")
    public void outputExport(@PathVariable("pId") Integer id,HttpServletResponse response) throws IOException {

            List<TGoods> list = tGoodsService.queryTGoodsList(id);
            List<ExportGoods> entities = list.stream().map(u -> {
            ExportGoods entity = new ExportGoods();
            BeanUtils.copyProperties(u, entity);
            return entity;
        }).collect(Collectors.toList());
        /**
         * 参数1：exportParams 导出配置对象
         * 参数2：导出的类型
         * 参数3：导出数据集合
         */
        ExportParams exportParams = new ExportParams();
        exportParams.setTitle("商品信息列表"); //表格标题
        exportParams.setSheetName("商品信息"); //sheet名
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, ExportGoods.class, entities);
        try {
            ServletOutputStream oos = response.getOutputStream();
            response.setHeader("content-type", "application/octet-stream");
            response.setHeader("content-disposition","attachment;fileName=" + URLEncoder.encode("goodsList.xlsx", "UTF-8"));
            workbook.write(oos);
            oos.flush();
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @AddSysLog
    @ResponseBody
    @PostMapping("/inputExport")
    public R easypoiImport(@RequestParam("file") MultipartFile file){
        R r = new R();
        if (ObjectUtils.isEmpty(file)) {
            r.error().message("file is null!!!");
            return r;
        }
        tGoodsService.easyImport(file, r);
        return r;
    }
}
